package demo.practice.stack;

import java.util.Stack;

public class P844 {
    /*"y#fo##f"
"y#f#o##f"*/

    public static void main(String[] args) {
        P844 p844 = new P844();
        p844.backspaceCompare("y#fo##f", "y#f#o##f");
    }

    public boolean backspaceCompare(String S, String T) {

        return getFinalStr(S).equals(getFinalStr(T));
    }

    private String getFinalStr(String S) {
        if (S == null || S.length() == 0)
            return S;
        Stack<Character> stack = new Stack<>();
        for (char c : S.toCharArray()) {
            if (c == '#') {
                if (stack.size() != 0)
                    stack.pop();
            } else
                stack.push(c);
        }

        StringBuffer sb = new StringBuffer();
        for (char c : stack) {
            sb.append(c);
        }
        return sb.toString();
    }
}
